package com.base.dehong.base.application;

import java.io.File;

import com.base.dehong.util.LogUtils;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StrictMode;

public class BaseApp extends Application {
	public static ImageLoader instance;
	
	@Override
	public void onCreate() {
		super.onCreate();
		initImageLoader(getApplicationContext());
 	}
	
	/**
	 * @describe ImageLoader初始化
	 */
	public static void initImageLoader(Context context) {
		// This configuration tuning is custom. You can tune every option, you
		// may tune some of them,
		// or you can create default configuration by
		// ImageLoaderConfiguration.createDefault(this);
		// method.
		
		String cacheDisc= Environment
				.getExternalStorageDirectory().getAbsolutePath()+"/Mall/Cache/images/";
				// 获取本地缓存的目录，该目录在SDCard的根目录下
				File cacheDir = StorageUtils.getOwnCacheDirectory(context, cacheDisc);
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.threadPoolSize(5) // 设置线程数量
				.memoryCache(new WeakMemoryCache())// 设定内存缓存为弱缓存
				.diskCacheFileNameGenerator(new Md5FileNameGenerator())// 设定缓存到SDCard目录的文件命名
				.denyCacheImageMultipleSizesInMemory()// 设定只保存同一尺寸的图片在内存
//				.memoryCacheExtraOptions(480, 800)// 设定内存图片缓存大小限制，不设置默认为屏幕的宽高
				.diskCacheSize(50 * 1024 * 1024)
				// 50 Mb
//				.discCache(new UnlimitedDiscCache(cacheDir))
				.diskCache(new UnlimitedDiscCache(cacheDir))
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				// 设定网络连接超时 timeout: 20s 读取网络连接超时read timeout: 60s
				.imageDownloader(new BaseImageDownloader(context, 20000, 60000))
//				.writeDebugLogs() // Remove for release app
				.build();
		instance = ImageLoader.getInstance();
		instance.init(config);
	}
	
    
	
}
